package com.bz.sunmipay.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.baizhi.baseapp.activity.BaseLoadingActivity;
import com.bz.sunmipay.R;
import com.bz.sunmipay.adapter.PaymentDetailAdapter;
import com.bz.sunmipay.controller.GlobalDataHolder;
import com.bz.sunmipay.database.DBHelper;
import com.bz.sunmipay.database.PaymentDetailDatabase;
import com.bz.sunmipay.model.PaymentDetail;
import com.bz.sunmipay.utils.IntentUtils;
import com.bz.sunmipay.utils.ThreadPoolUtil;

import java.util.List;

/**
 * 支付明细
 * <p>
 * Created by Administrator on 2019/12/6.
 */
public class PerformanceActivity extends BaseLoadingActivity {

    private PaymentDetailAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLoadingContent(R.layout.activity_performance);
        setLeftContentIcon(R.mipmap.back);
        setRightContentIcon(R.mipmap.home);
        setCenterText("PAYMENT");
        initView();

        loadData();
    }

    private void initView() {
        ListView lv = findViewById(R.id.payment_list);
        mAdapter = new PaymentDetailAdapter(this);
        lv.setAdapter(mAdapter);
    }

    //查数据库
    public void loadData() {
        ThreadPoolUtil.executeInSinglePool(new Runnable() {
            @Override
            public void run() {
                final List<PaymentDetail> data = getData();
                if (!isFinishing()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (data != null && !data.isEmpty()) {
                                showContentState();
                                mAdapter.setItems(data);
                                mAdapter.notifyChangedWithNoUpdate();
                                return;
                            }
                            showEmptyState();
                        }
                    });
                }
            }
        });
    }

    private List<PaymentDetail> getData() {
        PaymentDetailDatabase database = new PaymentDetailDatabase(this);
        return database.queryAllByUserName(DBHelper.TABLE_NAME, GlobalDataHolder.getInstance(this).getUserInfo().getNickname());
    }

    @Override
    protected void handleClickEvent(int event) {
        super.handleClickEvent(event);
        if (event == RIGHT_BUTTON) {
            IntentUtils.goToHome(this);
        }
    }
}
